package com.shigen.demo.config;


import com.shigen.demo.referer.interceptor.ResourceInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Configuration
@EnableWebMvc
@Slf4j
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private UserArgumentResolver userArgumentResolver;

    @Resource
    private ResourceInterceptor resourceInterceptor;

    /**
     * 静态资源保存目录
     */
    public static final String FILE_RESOURCE_PATH = "file:" + System.getProperty("user.dir") + "/files/";

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userArgumentResolver);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(resourceInterceptor).addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.error("静态资源保存目录:{}", FILE_RESOURCE_PATH);
        registry.addResourceHandler("/files/**")
                .addResourceLocations(FILE_RESOURCE_PATH);
    }
}
